package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.RolePermission;
import com.xmy.cultivate.entity.StaffPermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-09-25
 */
public interface StaffPermissionMapper extends BaseMapper<StaffPermission> {

    /**
     * 查询个人权限
     *
     * @param staffId
     * @return
     */
    @Select({"<script>",
            "SELECT DISTINCT permission_id ",
            "FROM ",
            "role_permission ",
            "WHERE ",
            "role_id IN ( ",
            "SELECT ",
            "role_id ",
            "FROM ",
            "role_admin ",
            "WHERE ",
            "admin_id = ( SELECT admin_id FROM staff WHERE id = ${staffId} )) ",
            "AND permission_id NOT IN ( SELECT permission_id FROM staff_permission WHERE staff_id = ${staffId} AND type = 2 ) ",
            "UNION ",
            "SELECT permission_id ",
            "FROM ",
            "staff_permission",
            "WHERE ",
            "staff_id = ${staffId} ",
            "AND type = 1 ",
            "</script>"})
    public List<Long> getStaffPermissionAllByStaffId(@Param("staffId") String staffId);

    /**
     * 通过角色查询个人权限
     *
     * @param staffId
     * @return
     */
    @Select({"<script>",
            "SELECT DISTINCT permission_id ",
            "FROM ",
            "role_permission ",
            "WHERE ",
            "role_id IN (${roleId}) ",
            "AND permission_id NOT IN ( SELECT permission_id FROM staff_permission WHERE staff_id = ${staffId} AND type = 2 ) ",
            "UNION ",
            "SELECT permission_id ",
            "FROM ",
            "staff_permission",
            "WHERE ",
            "staff_id = ${staffId} ",
            "AND type = 1 ",
            "</script>"})
    public List<Long> getStaffPermissionAll(@Param("staffId") String staffId, @Param("roleId") String roleId);

    @Select("SELECT permission_id FROM staff_permission WHERE staff_id = ${staffId} AND type = ${type} ")
    public List<Long> getStaffPermissionByStaffId(@Param("staffId") String staffId, @Param("type") Integer type);

    @Select("SELECT permission_id FROM staff_permission WHERE staff_id = ${staffId} ")
    public List<Long> getStaffPermissionByStaffId(@Param("staffId") String staffId);

}
